let App = getApp()
var wxParse = require("../../wxParse/wxParse.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: [],
    current: 0,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 500,
    showServer: false,
    showSpec: false,
    showCar: false,
    specs: '规格',
    say: "祝你生日快乐",
    phone: "",
    num: 1,
    id: 0,
    info: {},
    modellist: [],
    weightlist: [],
    disposelist: [],
    spec_value: "",
    goods_weight: "",
    dispose: "",
    tishi: "",
    goods_spec_id: 0,
    cart_total_num: 0,
    cartlist: [],
    paymoney: 0,
    imgurl: "",
    ok: false,
    type: "",
    fileList: [],
    zhufu: "",
    phone: "",
    descs: ""

  },
  changeIndex(e) {
    console.log(e)
    let index = e.detail.current;
    this.setData({
      current: index
    })
  },
  specifications(e) {
    let type = e.currentTarget.dataset.type
    if (type === 'server') {
      this.setData({
        showServer: true
      })
    } else if (type === 'spec') {
      this.check();
      this.setData({
        showSpec: true
      })
    } else {
      this.setData({
        showCar: true
      })
    }

  },
  onClose(e) {
    console.log(e)
    let type = e.currentTarget.dataset.type
    if (type === 'server') {
      this.setData({
        showServer: false
      })
    } else if (type === 'spec') {
      this.setData({
        showSpec: false
      })
    } else {
      this.setData({
        showCar: false
      })
    }
  },
  submit(e) {

    this.setData({
      type: e.currentTarget.dataset.type
    })
    if (this.data.modellist.length > 0) {
      console.log("ss", this.data.modellist);
      this.setData({
        spec_value: this.data.modellist[0].spec_value,
        imgurl: this.data.modellist[0].imgurl
      })
    }

    this.setData({
      showSpec: true
    })
    this.check();


  },
  buy() {
    var _this = this;

    if (this.data.modellist.length > 0 && this.data.spec_value == "") {

      this.setData({
        tishi: "请选择规格",
        showSpec: true
      })

      return;
    }

    if (this.data.weightlist.length > 0 && this.data.goods_weight == "") {

      this.setData({
        tishi: "请选择尺寸",
        showSpec: true
      })

      return;
    }
    if (this.data.disposelist.length > 0 && this.data.dispose == "") {

      this.setData({
        tishi: "请选择配置",
        showSpec: true
      })
      return;
    }

    let type = this.data.type;
    if (type == "incar") {
      this.cartAdd();
      this.setData({
        showSpec: false
      })
      return;
    }
    var fapiao = "";
    if (_this.data.fileList != "" && _this.data.fileList.length > 0) {
      fapiao = _this.data.fileList[0].url;
    }

    wx.navigateTo({
      url: "/pages/order/orderSubmit/orderSubmit?" + App.urlEncode({
        goods_id: this.data.info.id,
        goods_name: this.data.info.goods_name,
        num: this.data.num,
        goods_spec_id: this.data.goods_spec_id,
        imgurl: this.data.imgurl,
        goods_attr: this.data.spec_value,
        goods_price: this.data.paymoney,
        zhufu: _this.data.zhufu,
        phone: _this.data.phone,
        descs: _this.data.descs,
        fapiao: fapiao
      })
    });
  },
  gohome() {
    wx.reLaunch({
      url: '/pages/index/index',
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      id: options.id
    })
    this.getGoodsDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    if (wx.getStorageSync('token')) {
      this.getCartNum()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getGoodsDetail() {
    let _this = this;
    App._post_form('wxapi/goods/detail', {
      id: _this.data.id
    }, function (result) {
      // 初始化商品详情数据

      _this.setData({
        info: result.data,
        paymoney: result.data.money,
        background: result.data.imglist
      });

      console.log("??????????", result.data.content.length);
      if (result.data.content.length > 0) {
        wxParse.wxParse('content', 'html', result.data.content, _this, 0);
      }
      var items = result.data.spec;
      let obj = {};
      let peon = items.reduce((cur, next) => {

        obj[next.spec_value] ? "" : obj[next.spec_value] = true && cur.push(next);
        return cur;
      }, []) //设置cur默认类型为数组，并且初始值为空的数组
      console.log(peon);

      console.log("款式", peon);

      _this.setData({
        modellist: peon
      })

      let obj1 = {};
      let peon1 = items.reduce((cur, next) => {
        obj1[next.goods_weight] ? "" : obj1[next.goods_weight] = true && cur.push(next);
        return cur;
      }, []) //设置cur默认类型为数组，并且初始值为空的数组
      if (peon1.length > 0) {
        if (peon1[0].goods_weight) {
          _this.setData({
            weightlist: peon1
          })
        }
      }


      console.log("尺寸", peon1);
      let obj2 = {};
      let peon2 = items.reduce((cur, next) => {
        obj2[next.dispose] ? "" : obj2[next.dispose] = true && cur.push(next);
        return cur;
      }, []) //设置cur默认类型为数组，并且初始值为空的数组
      if (peon2.length > 0) {
        if (peon2[0].dispose) {
          _this.setData({
            disposelist: peon2
          })
        }
      }

      console.log("配置", peon2);
    });
  },
  choosemodel(e) {
    let id = e.currentTarget.dataset.id;
    var imgurl = e.currentTarget.dataset.imgurl;
    console.log("fffffffffffffffff", id, e);
    this.setData({
      spec_value: id,
      imgurl: imgurl
    })
    this.check();
  },
  chooseweight(e) {
    let id = e.currentTarget.dataset.id;
    console.log("fffffffffffffffff", id, e);
    this.setData({
      goods_weight: id
    })
    this.check();
  },
  choosedispose(e) {
    let id = e.currentTarget.dataset.id;
    console.log("fffffffffffffffff", id, e);
    this.setData({
      dispose: id
    })
    this.check();
  },
  check() {
    var that = this;
    if (this.data.modellist.length > 0 && this.data.spec_value == "") {
      this.setData({
        tishi: "请选择款式"
      })
      return;
    }

    if (this.data.weightlist.length > 0 && this.data.goods_weight == "") {
      this.setData({
        tishi: "请选择尺寸"
      })
      return;
    }
    if (this.data.disposelist.length > 0 && this.data.dispose == "") {
      this.setData({
        tishi: "请选择配置"
      })
      return;
    }
    console.log("ttt", this.data.info);
    var items = this.data.info.spec;
    var money = 0;
    var goods_spec_id = 0;

    console.log("金额计算", this.data.goods_spec_id, this.data.goods_weight, this.data.dispose, items);
    for (var i = 0; i < items.length; i++) {
      console.log("对比1", items[i].spec_value, this.data.spec_value)
      console.log("对比1", items[i].goods_weight, this.data.goods_weight)
      console.log("对比1", items[i].dispose, this.data.dispose)
      if (items[i].spec_value == this.data.spec_value && items[i].goods_weight == this.data.goods_weight && items[i].dispose == this.data.dispose) {
        money = items[i].goods_price;
        goods_spec_id = items[i].id;
        break;
      }
    }
    this.setData({
      money: money,
      goods_spec_id: goods_spec_id,
      paymoney: money * that.data.num,
    })

    this.setData({
      tishi: "已选择  " + this.data.spec_value + " " + this.data.goods_weight + "  " + this.data.dispose,
      ok: true
    })

    this.getmoney();

  },
  cartAdd: function () {
    let _this = this;

    var fapiao = "";
    if (_this.data.fileList != "" && _this.data.fileList.length > 0) {

      fapiao = _this.data.fileList[0].url;
    }

    App._post_form('wxapi/cart/add', {
      goods_id: _this.data.id,
      goods_spec_id: _this.data.goods_spec_id,
      num: _this.data.num,
      goods_attr: _this.data.spec_value,
      goods_weight: _this.data.goods_weight,
      dispose: _this.data.dispose,
      zhufu: _this.data.zhufu,
      phone: _this.data.phone,
      descs: _this.data.descs,
      fapiao: fapiao
    }, function (result) {
      if (result.status == "200") {


        setTimeout(() => {
          App.showSuccess("加入成功");
          _this.getCartNum()
        }, 1000);
        // 

      }
    });

  },
  getCartNum() {
    var that = this;
    App._post_form('wxapi/cart/lists', {}, res => {

      var list = res.data.list;
      let num = list.reduce((a, b) => {
        return a + b.num
      }, 0)

      this.setData({
        cart_total_num: num,
        cartlist: list
      })
    })
  },
  numChange(event) {
    console.log("ss", this.data.money);
    console.log(event.detail);
    this.setData({
      num: event.detail
    })
    this.getmoney();

  },
  getmoney() {
    this.setData({
      paymoney: this.data.money * this.data.num,
    })
  },
  onShareAppMessage() {
    var id = this.data.id;
    return {
      title: this.data.info.goods_name,
      path: `pages/goods/goods?id=${id}`
    }
  },
  onShareTimeline() {
    var id = this.data.id;
    return {
      title: this.data.info.goods_name,
      query: '?id=' + id,
      imageUrl: this.data.info.imgurl
    }

  },
  afterRead(event) {
    var that = this;
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: App.api_root + 'upload/file', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'imgFile',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        console.log("ss", JSON.parse(res.data).pic);

        that.setData({
          fileList: [{
            url: JSON.parse(res.data).pic,
            isImage: true,
            deletable: true,
            name: '图片',
          }]
        });
      },
    });
  },
  delimg() {
    this.setData({
      fileList: []
    });
  }
})